from django.db import models

# Neede from NoPBX
from nopbx.core.models import BaseModule
from nopbx.dialplan.models import Extension
from nopbx.account.models import Person

###############################################################################

class ContextManager(models.Manager):
    def get_by_natural_key(self, name):
        return self.get(name = name)

class Context(BaseModule):
    """
    Voicemail box contexts
    """
    name = models.CharField(max_length=64)
    path = models.CharField(max_length=255, blank = True, null = True, help_text = 'Storage path for this voicemail context.')

    objects = ContextManager()
    class Meta:
        ordering = ['name']

    def __unicode__(self):
        # Have to force this to a string, not an object
        return '%s' % self.name

    def natural_key(self):
        return self.name



###############################################################################

class MailboxManager(models.Manager):
    def get_by_natural_key(self, extension):
        return self.get(extension = extension)

class Mailbox(BaseModule):
    """
    Actual voicemail box (Tied specifically to an extension)
    """
    YES_NO_CHOICES = (('Yes', 'Enabled'), ('No', 'Disabled'))

    context   = models.ForeignKey(Context)
    extension = models.ForeignKey(Extension)
    password  = models.CharField(max_length=20)

    person    = models.ForeignKey(Person, blank = True, null = True)
    notify    = models.BooleanField(default = True, help_text = 'Send an email notification when a new message is recieved.')

    # fullname
    # email
    # pager
    # tz

    attach          = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'No',  verbose_name = 'Attach',                help_text = 'Attach the recording when sending an email notification.')
    # attachfmt
    deletevoicemail = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'No',  verbose_name = 'Delete',                help_text = 'Delete the voicemail after sending the email notification (Only useful if Attach = Yes).')
    saycid          = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'No',  verbose_name = 'Say Caller-ID',         help_text = 'Say the Caller-ID when playing back the message.')
    review          = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'Yes', verbose_name = 'Review Msg',            help_text = 'Allow messages to be reviewed before sending.')
    operator        = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'Yes', verbose_name = 'Operator',              help_text = 'Enable users to reach an operator by pressing Zero.')
    envelope        = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'No',  verbose_name = 'Msg Envelope',          help_text = 'Say the time and date when the message was recieved during playback.')
    sayduration     = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'yes', verbose_name = 'Say Duration',          help_text = 'Say how long the message is before playback.')
    # saydurationm
    sendvoicemail   = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'No',  verbose_name = 'Send Msg',              help_text = 'Enable users to send a voicemail to another mailbox from within their mailbox.')
    forcename       = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'Yes', verbose_name = 'Force Name',            help_text = 'Force users to record their name.')
    forcegreetings  = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'Yes', verbose_name = 'Force Greetings',       help_text = 'Force users to record their greetings.')
    hidefromdir     = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'No',  verbose_name = 'Hide from Directory',   help_text = 'Do not show this mailbox in the directory.')
    tempgreetwarn   = models.CharField(max_length = 3, choices = YES_NO_CHOICES, default = 'Yes', verbose_name = 'Temp Greeting Warning', help_text = 'Warn user that they have a temporary greeting set when they login to voicemail.')

    stamp           = models.DateTimeField(auto_now = True)

    objects = MailboxManager()
    class Meta:
        ordering = ['context', 'extension']
        unique_together = ('context', 'extension')
        verbose_name_plural = 'Mailboxes'

    def __unicode__(self):
        # Have to force this to a string, not an object
        return '%s' % self.extension

    def natural_key(self):
        return self.extension


